﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using TTGame;

/*
 * BuffContainer
 * Buff容器
 *
 * 绑放在人物身上，存放人物身上的所有Buff
 *
 *
 * 依赖：
 *
 * ！不写重复代码！
 * @author TTun
 * @date 8/30/2017 9:59:19 AM
 */
namespace TTGameEngine {

public class BuffContainer : MonoBehaviour {
    public Puppet m_owner;
    //<BuffID,Buff>
    Dictionary<int, Buff> m_activeBuffsDict = new Dictionary<int, Buff>();
    List<int> t_removedBuffIDs = new List<int>();

#if UNITY_EDITOR
    [SerializeField]
    List<string> Debug_ShowBuffs = new List<string>();
#endif

    void Awake() {
        m_owner = GetComponent<Puppet>();
    }

    // Update is called once per frame
    void Update() {
        float dt = Time.deltaTime;
        t_removedBuffIDs.Clear();
        for(var ite = m_activeBuffsDict.GetEnumerator(); ite.MoveNext();) {
            Buff buff = ite.Current.Value;
            if(false == buff.UpdateBuff(dt)) {
                t_removedBuffIDs.Add(buff.BuffID);
#if UNITY_EDITOR
                Debug_ShowBuffs.Remove(buff.GetDumpInfoStr());
#endif
            }
        }

        for(int i = 0; i < t_removedBuffIDs.Count; i++) {
            m_activeBuffsDict.Remove(t_removedBuffIDs[i]);
        }

    }
    public void AddBuff(int buffID, string casterGID, int skillID, uint skillCastID) {
        Buff buff = null;
        m_activeBuffsDict.TryGetValue(buffID, out buff);
        if(buff == null) {
            buff = BuffManager.Share().ObtainBuff(buffID);
            AddBuff(buff, casterGID, skillID, skillCastID);
        } else {
            buff.Fresh();
        }
    }

    void AddBuff(Buff buff, string casterGID, int skillID, uint skillCastID) {
        if(buff == null) {
            return;
        }

        bool isRunning = buff.Trigger(m_owner, casterGID, skillID, skillCastID);

        if(isRunning == true) {
            m_activeBuffsDict.Add(buff.BuffID , buff);
#if UNITY_EDITOR
            Debug_ShowBuffs.Add(buff.GetDumpInfoStr());
#endif
            switch(buff.GetBuffType()) {
                case BuffType.PropBuff:

                    break;
                case BuffType.HPBuff:

                    break;
                case BuffType.ShieldBuff:

                    break;
            }

        }
    }

    /// <summary>
    /// 因Buff变化的数值
    /// </summary>
    /// <param name="originValue"></param>
    /// <returns></returns>
    public float GetPropChangeValue(int PropID, float originValue) {
        float result = 0;
        for(var ite = m_activeBuffsDict.GetEnumerator(); ite.MoveNext();) {
            result += ite.Current.Value.GetPropChangeValue(PropID, originValue);
        }
        return result;
    }

    /// <summary>
    /// 应用Buff对受到伤害的影响。
    ///
    /// </summary>
    /// <param name="dmg">是正数</param>
    /// <returns></returns>
    public float ApplyReceiveDamage(float dmg) {
        if(dmg < 0) {
            return dmg;
        }
        float dmgResult = dmg;
        for(var ite = m_activeBuffsDict.GetEnumerator(); ite.MoveNext();) {
            dmgResult = ite.Current.Value.ApplyReceiveDamage(dmgResult);
            if(dmgResult <= 0) {
                break;
            }
        }
        return dmgResult;
    }

    /// <summary>
    /// 应用Buff对受到伤害的影响。
    ///
    /// </summary>
    /// <param name="dmg">是正数</param>
    /// <returns></returns>
    public float ApplyAttackDamage(float dmg) {
        if(dmg < 0) {
            return dmg;
        }
        float dmgResult = dmg;
        for(var ite = m_activeBuffsDict.GetEnumerator(); ite.MoveNext();) {
            dmgResult = ite.Current.Value.ApplyAttackDamage(dmgResult);
            if(dmgResult <= 0) {
                break;
            }
        }
        return dmgResult;
    }

    public void Clear() {
        for(var ite = m_activeBuffsDict.GetEnumerator(); ite.MoveNext();) {
            Buff buff = ite.Current.Value;
            if(buff != null) buff.ClearBuff();
        }
        m_activeBuffsDict.Clear();
#if UNITY_EDITOR
        Debug_ShowBuffs.Clear();
#endif
    }
}

}
